class MockUserService {
    constructor() {
        this.users = this.generateMockData(35);
        console.log('users :',this.users)
        this.nextId = this.users.length + 1;
    }

    // 生成模拟数据
    generateMockData(count) {
        const mockData = [];
        const surnames = ['王', '李', '张', '刘', '陈', '杨', '赵', '黄'];
        const names = ['伟', '芳', '强', '敏', '磊', '丽', '军', '艳'];
        const sexList = ['0', '1'];

        for (let i = 1; i <= count; i++) {
            const sex = sexList[Math.floor(Math.random() * sexList.length)];
            mockData.push({
                id: i,
                name: surnames[Math.floor(Math.random() * surnames.length)] +
                    names[Math.floor(Math.random() * names.length)],
                age: String (Math.floor(Math.random() * (50 - 18 + 1)) + 18),
                height: sex === '0' ?
                    Math.floor(Math.random() * (190 - 165 + 1)) + 165 :
                    Math.floor(Math.random() * (175 - 150 + 1)) + 150,
                sex: sex,
                email:'test@qq.com'
            });
        }
        return mockData;
    }

    // 分页查询
    query(params) {
        console.log('列表：',params)
        const {pageNum,pageSize} = params
        return new Promise(resolve => {
            const start = (pageNum - 1) * pageSize;
            const end = start + pageSize;
            resolve({
              data: this.users.slice(start, end),
              page: {
                pageSize: pageSize,
                totalCount: this.users.length,
                pageIndex: Math.ceil(this.users.length / pageSize)
              }

            })
        })

    }

    // 添加用户
    add(user) {
        console.log('新增 :',user)
        return new Promise(resolve => {
            const newUser = {
                id: this.nextId++,
                name: String(user.name || ''),
                age: Number(user.age) || 18,
                height: Number(user.height) || 160,
                sex: ['0', '1'].includes(user.sex) ? user.sex : '0'
            };
            this.users = [newUser,...this.users];
            resolve({data:newUser})
        })

    }

    // 删除用户
    delete(id) {
      console.log('删除 :',id)
      return new Promise(resolve => {
        const index = this.users.findIndex(u => u.id === id);
        if (index === -1) {
          resolve({data:false})
          return
        }
        this.users.splice(index, 1);
        resolve({data:true})
      })

    }

    // 更新用户
    update(e) {
        console.log('更新 :',e)
        return new Promise(resolve => {
            const user = this.users.find(u => u.id === e?.id);
            if (!user) return null;
            const allowedUpdates = ['name', 'age', 'height', 'sex'];
            Object.keys(e).forEach(key => {
                if (allowedUpdates.includes(key)) {
                    user[key] = e[key]
                }
            });
            console.log('更新后：',this.users)
           resolve( {data:{...user}})

        })
    }

    // 查询全部
    queryAll() {
      return new Promise(resolve => {
        resolve( {data:[...this.users]})
      })


    }

    // ID查询
    queryById(id) {
      return new Promise(resolve => {
        resolve( {data:this.users.find(u => u.id === id) || null})
      })



    }

    querySex() {
        return new Promise(resolve => {
            resolve( {data:[
                {dictValue:'0',dictLabel:'男'},
                {dictValue:'1',dictLabel:'女'}
            ]})
        })

    }
}

export default new MockUserService()

